package cn.com.thinvent.dispatch.web.controller;

import cn.com.thinvent.dispatch.entity.DailyExpenses;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.entity.response.ReturnModel;
import cn.com.thinvent.dispatch.service.biz.DailyExpensesService;
import cn.com.thinvent.dispatch.service.biz.util.KeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/daily-expenses")
public class DailyExpensesController {

    @Autowired
    private DailyExpensesService dailyExpensesService;

    @GetMapping
    public ReturnModel getAllDailyExpenses(@RequestParam(defaultValue = "1") int page,
                                           @RequestParam(defaultValue = "10") int pageSize,
                                           @RequestParam(required = false) String expenseItem) {
        PagedGridResult<DailyExpenses> pagedResult = dailyExpensesService.getAllDailyExpenses(page, pageSize, expenseItem);
        return new ReturnModel(200, pagedResult, "Success");
    }

    @GetMapping("/{id}")
    public ReturnModel getDailyExpensesById(@PathVariable String id) {
        DailyExpenses dailyExpenses = dailyExpensesService.getDailyExpensesById(id);
        return new ReturnModel(200, dailyExpenses, "Success");
    }

    @PostMapping
    public ReturnModel addDailyExpenses(@RequestBody DailyExpenses dailyExpenses) {
        dailyExpenses.setId(KeyGenerator.generateKey());
        Date date = new Date();
        dailyExpenses.setCreatedAt(date);
        dailyExpenses.setUpdatedAt(date);
        dailyExpensesService.addDailyExpenses(dailyExpenses);
        return new ReturnModel(201, null, "Created");
    }

    @PutMapping("/{id}")
    public ReturnModel updateDailyExpenses(@PathVariable String id, @RequestBody DailyExpenses dailyExpenses) {
        dailyExpenses.setId(id);
        dailyExpensesService.updateDailyExpenses(dailyExpenses);
        return new ReturnModel(200, null, "Updated");
    }

    @DeleteMapping("/{id}")
    public ReturnModel deleteDailyExpenses(@PathVariable String id) {
        dailyExpensesService.deleteDailyExpenses(id);
        return new ReturnModel(200, null, "Deleted");
    }
}
